<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="65%">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="108px"
      >
        <el-form-item label="服务单元名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入服务单元名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="服务区域" prop="serviceArea">
          <el-input
            v-model="queryParams.serviceArea"
            placeholder="请输入服务区域"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table
        row-key="id"
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        @selection-change="handleRowCheckboxChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="服务单元名称" align="center" prop="name" />
        <el-table-column label="所属部门" align="center" prop="belongDepart" />
        <el-table-column label="服务类型" align="center" prop="serviceType" />
        <el-table-column label="是否测试单元" align="center" prop="isTestUnit" />
        <el-table-column label="是否发起收款任务" align="center" prop="isReceiveMoney" />
        <el-table-column label="运维类合同收款负责人类型" align="center" prop="receiveMoneyType" />
        <el-table-column label="服务区域" align="center" prop="serviceArea" />
        <el-table-column label="驻点地址" align="center" prop="address" />
        <el-table-column label="操作" align="center" min-width="120px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="openForm('update', scope.row.id)"
              v-hasPermi="['san:biz-customer-archive-service-unit:update']"
            >
              编辑
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-hasPermi="['san:biz-customer-archive-service-unit:delete']"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>

    <template #footer>
      <el-button @click="submit" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import {
  CustomerArchiveServiceUnitApi,
  CustomerArchiveServiceUnit
} from '@/api/biz/customer/serviceunit'
import ServiceUnitForm from './ServiceUnitForm.vue'

/** 客户档案-服务单元管理 列表 */
defineOptions({ name: 'CustomerArchiveServiceUnit' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CustomerArchiveServiceUnit[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  serviceArea: undefined,
  ownerUserId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CustomerArchiveServiceUnitApi.getCustomerArchiveServiceUnitPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = '服务单元'
  handleQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const serviceUnitData = ref({})
const handleRowCheckboxChange = (records: CustomerArchiveServiceUnit[]) => {
  serviceUnitData.value = records[0]
}

const submit = () => {
  dialogVisible.value = false
  emit('success', serviceUnitData.value)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
